layui.define(['table', 'form', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    // 专家列表
    table.render({
        elem: '#LAY-expert-manage'
        , url: '/admin/signin/index_handle'
        , cols: [[
            { field: 'id', width: 80, title: 'ID', sort: true, fixed: true }
            , { field: 'name', width: 150, title: '奖品', fixed: true }
            , { field: 'quantity', width: 80, title: '数量', fixed: true }
            , {
                field: '', title: '缩略图', width: 120, templet: function (d) {
                    return '<img style="display: inline-block;height: 100%;" src= "' + d.thumb + '" layadmin-event="iconPreview" value= "' + d.thumb + '">';
                }
            }
            , { field: 'stage', title: '关卡', width: 100 }
            , { field: 'rank', title: '奖品等级', width: 100 }
            , {
                field: '', title: '奖品类型', width: 100, templet: function (d) {
                    if (d.type == 1) {
                        return '虚拟';
                    }
                    if (d.type == 2) {
                        return '<font color="red">实物</font>';
                    }
                    if (d.type == 3) {
                        return '抽奖机会';
                    }
                    if (d.type == 4) {
                        return '进退步数';
                    }
                    if (d.type == 5) {
                        return '龙钻';
                    }
                    if (d.type == 6) {
                        return '金币';
                    }
                    if (d.type == 7) {
                        return '经验值';
                    }
                    if (d.type == 105) {
                        return '观赛券';
                    }
                }
            },
            {
                title: "操作",
                width: 200,
                align: "right",
                fixed: "right",
                toolbar: "#table-expertlist",
            },
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true,
    });

    //监听工具条
    table.on('tool(LAY-expert-manage)', function (obj) {
        if (obj.event === 'edit') {
            var index = layer.open({
                type: 2
                , title: '编辑关卡奖励'
                , content: '/admin/signin/edit?id=' + obj.data.id
                , maxmin: true
                , area: ['900px', '550px']
            });
        }

        if (obj.event === 'delete') {
            layer.confirm('确定要删除吗？', function (index) {
                admin.req({
                    url: '/admin/signin/delete?id=' + obj.data.id
                    , data: obj.field
                    , success: function (res) {
                        if (res.code == 0) {
                            layer.closeAll("loading");
                            window.location.reload();
                            table.reload('LAY-expert-manage');
                        }
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    }
                });
            });
            return false;
        }

    });

    form.on('submit(LAY-signin-add)', function (obj) {
        layer.open({
            type: 2
            , title: '添加奖励关卡'
            , content: '/admin/signin/add'
            , maxmin: true
            , area: ['900px', '550px']
        });
    });

    form.on('submit(expert-add)', function (obj) {
        //提交
        admin.req({
            url: '/admin/expert/add_post'
            , data: obj.field
            , success: function (res) {
                if (res.code == 0) {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    parent.layer.msg(res.msg, { icon: 1 });
                    parent.layui.table.reload('LAY-expert-manage');
                }
            }
        });
        return false;
    });

    form.on('submit(expert-edit)', function (obj) {
        //提交
        admin.req({
            url: '/admin/expert/edit_post'
            , data: obj.field
            , success: function (res) {
                if (res.code == 0) {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    parent.layer.msg(res.msg, { icon: 1 });
                    parent.layui.table.reload('LAY-expert-manage');
                }
            }
        });
        return false;
    });


    //上传头像
    var iconSrc = $('#LAY_iconImgSrc');
    var iconPreview = $('#coverImgPreview');
    upload.render({
        url: '/admin/index/noOperation'
        , elem: '#LAY_iconImgUpload'
        , auto: true
        , choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: '/admin/index/signForCos'
                , success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime
                                });
                            }
                        });
                        cos.putObject({
                            Bucket: res.data.bucket,
                            Region: res.data.region,
                            Key: 'images/' + res.data.filename + choosefile.name,
                            Body: choosefile,
                        }, function (err, data) {
                            layer.closeAll('loading');
                            if (err) {
                                console.log(err);
                                layer.msg("上传失败", { icon: 5 });
                            } else {
                                if (data.statusCode == 200) {
                                    iconSrc.val("https://" + data.Location);
                                    iconPreview.attr("value", iconSrc.val());
                                } else {
                                    layer.msg("图片上传失败", { icon: 5 });
                                }
                            }
                        });
                    } else {
                        layer.closeAll('loading');
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
                complete: function () {
                    layer.closeAll('loading');
                }
            })
        }
    });

    //查看头像
    admin.events.iconPreview = function (othis) {
        var src = othis.attr("value");
        if (!src) {
            src = iconSrc.val();
        }
        layer.photos({
            photos: {
                "title": "查看" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    table.render({
        elem: '#LAY-signin-history'
        , url: '/admin/signin/history_handle'
        , cols: [[
            { field: 'id', width: 80, title: 'ID', sort: true, fixed: true }
            , { field: 'name', width: 150, title: '奖品', fixed: true }
            , { field: 'quantity', width: 80, title: '数量', fixed: true }
            , {
                field: '', title: '缩略图', width: 120, templet: function (d) {
                    return '<img style="display: inline-block;height: 100%;" src= "' + d.thumb + '" layadmin-event="iconPreview" value= "' + d.thumb + '">';
                }
            }
            , { field: 'stage', title: '关卡', width: 100 }
            , { field: 'rank', title: '奖品等级', width: 100 }
            , {
                field: '', title: '奖品类型', width: 100, templet: function (d) {
                    console.log(d.type, "---------", d.type == 105, "******", d.type == "105")
                    if (d.type == 1) {
                        return '虚拟';
                    }
                    if (d.type == 2) {
                        return '<font color="red">实物</font>';
                    }
                    if (d.type == 3) {
                        return '抽奖机会';
                    }
                    if (d.type == 4) {
                        return '进退步数';
                    }
                    if (d.type == 5) {
                        return '龙钻';
                    }
                    if (d.type == 6) {
                        return '金币';
                    }
                    if (d.type == 7) {
                        return '经验值';
                    }
                    if (d.type == 105) {
                        return '观赛券';
                    }
                },
            }
            , { field: 'user_id', title: '用户ID', width: 150 },
            , { field: 'nickname', title: '用户', width: 150 },
            {
                field: "",
                width: 100,
                title: "登录设备",
                templet: function (d) {
                    switch (d.platform) {
                        case 0:
                            return '<span class="layui-badge layui-bg-red">WEB</span>';
                        case 1:
                            return '<span class="layui-badge layui-bg-blue">IOS</span>';
                        case 2:
                            return '<span class="layui-badge layui-bg-green">Android</span>';
                        default:
                            return " 未知";
                    }
                },
            },
            , { field: 'created_at', title: '时间', width: 200 }
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    exports('signin', {})
});


function formatTimeStamp(time_stamp) {
    const time = new Date(time_stamp * 1000);
    const Y = time.getFullYear();
    const M = (time.getMonth() + 1).toString().padStart(2, "0");
    const D = time.getDate().toString().padStart(2, "0");
    const h = time.getHours().toString().padStart(2, "0");
    const m = time.getMinutes().toString().padStart(2, "0");
    const s = time.getSeconds().toString().padStart(2, "0");
    return `${Y}-${M}-${D} ${h}:${m}:${s}`;
}